package com.controller;


import com.entity.BUser;
import com.entity.FAddress;
import com.entity.FOrder;
import com.entity.FUser;
import com.entity.table.BUserTableDef;
import com.entity.table.FAddressTableDef;
import com.entity.table.FOrderTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.FAddressService;
import com.service.FOrderService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

//用户中心
@Controller
public class UserController {
    @Autowired
    private FOrderService fOrderService;
    @Autowired
    FAddressService fAddressService;

    //用户基本信息
    @GetMapping("/user_center_info")
    public String user_center_info(HttpServletRequest request, ModelMap model) {
        //从内存中读取用户信息
        HttpSession session = request.getSession();
        FUser user = (FUser) session.getAttribute("user");
        model.addAttribute("user", user);
        return "client/user_center_info";
    }

    //收货地址页面
    @GetMapping("/user_center_site")
    public String user_center_site(HttpServletRequest request, ModelMap model) {
        //从内存中读取用户信息
        HttpSession session = request.getSession();
        FUser user = (FUser) session.getAttribute("user");
        //根据用户id查询用户收货地址
        FAddress fAddress = fAddressService.queryChain().where(FAddressTableDef.F_ADDRESS.USER_ID.eq(user.getId())).one();
        model.addAttribute("fAddress", fAddress);
        return "client/user_center_site";
    }

    //修改收货地址
    @PostMapping("/fAddess/update")
    public String update(FAddress fAddress, ModelMap model, HttpServletRequest request) {
        //从内存中读取用户信息
        HttpSession session = request.getSession();
        FUser user = (FUser) session.getAttribute("user");
        //
        fAddress.setUserId(user.getId());
        model.addAttribute("fAddress", fAddress);
        fAddressService.saveOrUpdate(fAddress);
        return "/client/user_center_site";
    }

    // 用户中心订单列表
    @GetMapping(value = "/user_center_order")
    public String userOrderList(ModelMap model, HttpServletRequest request,
                                @RequestParam(value = "p", defaultValue = "1") int p,
                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        try {
            HttpSession session = request.getSession();
            FUser user = (FUser) session.getAttribute("user");
            Page<FOrder> page = new Page<>();
            page.setPageSize(pageSize);//设置每页显示记录数
            page.setPageNumber(p);//设置页码
            //定义字典 key：商品分类名称，value：对应分类下的商品列表
            QueryWrapper wrapper = QueryWrapper.create().where(FOrderTableDef.F_ORDER.USER_ID.eq(user.getId()));
            Page<FOrder> orderList = fOrderService.getMapper().paginateWithRelations(page, wrapper);
            model.addAttribute("page", orderList);
            model.addAttribute("user", user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "client/user_center_order";
    }
}